$input a_color0, a_position, a_texcoord0, a_texcoord1
#ifdef INSTANCING
    $input i_data0, i_data1, i_data2
#endif
$output texcolor, v_fog, v_texcoord0, v_lightmapUV, v_worldPos, v_chunkPos, horizioncolor, cloudscolor, basecolor, ambientcolor

#include <bgfx_shader.sh>
#include <Fuctions_MLYX.glsl>

uniform vec4 RenderChunkFogAlpha;
uniform vec4 FogAndDistanceControl;
uniform vec4 ViewPositionAndTime;
uniform vec4 FogColor;

void main() {
    mat4 model;
#ifdef INSTANCING
    model = mtxFromCols(i_data0, i_data1, i_data2, vec4(0, 0, 0, 1));
#else
    model = u_model[0];
#endif

    vec3 worldPos = mul(model, vec4(a_position, 1.0)).xyz;
    vec4 color;
#ifdef RENDER_AS_BILLBOARDS
    worldPos += vec3(0.5, 0.5, 0.5);
    vec3 viewDir = normalize(worldPos - ViewPositionAndTime.xyz);
    vec3 boardPlane = normalize(vec3(viewDir.z, 0.0, -viewDir.x));
    worldPos = (worldPos -
        ((((viewDir.yzx * boardPlane.zxy) - (viewDir.zxy * boardPlane.yzx)) *
        (a_color0.z - 0.5)) +
        (boardPlane * (a_color0.x - 0.5))));
    color = vec4(1.0, 1.0, 1.0, 1.0);
#else
    color = a_color0;
#endif

    vec3 modelCamPos = (ViewPositionAndTime.xyz - worldPos);
    float camDis = length(modelCamPos);
    vec4 fogColor;
    fogColor.rgb = FogColor.rgb;
    fogColor.a = clamp(((((camDis / FogAndDistanceControl.z) + RenderChunkFogAlpha.x) -
        FogAndDistanceControl.x) / (FogAndDistanceControl.y - FogAndDistanceControl.x)), 0.0, 1.0);

#ifdef TRANSPARENT
    if(a_color0.a < 0.95) {
        color.a = mix(a_color0.a, 1.0, clamp((camDis / FogAndDistanceControl.w), 0.0, 1.0));
    };
#endif

    v_texcoord0 = a_texcoord0;
    v_lightmapUV = a_texcoord1;
    texcolor = color;
    v_fog = fogColor;
    v_worldPos = worldPos;
    v_chunkPos = a_position.xyz;

    bool InsuperiAquoris = FogAndDistanceControl.x==0.0&&FogAndDistanceControl.y<0.8&&(FogColor.b>FogColor.r||FogColor.g>FogColor.r);

    if(InsuperiAquoris){

        horizioncolor = vec3(0.2,0.3,0.5);
        basecolor = vec3(0.2,0.3,0.5);
        ambientcolor = vec3(1.0,1.0,1.0);
        cloudscolor = vec3(1.0,1.0,1.0);
 
    } else {
        float LloverDet = ViaALlover(FogAndDistanceControl.xyz);
        horizioncolor = CaeloHorizionte(LloverDet, FogColor.rgb);
        cloudscolor = ColorNubis(LloverDet, FogColor.rgb);
        basecolor = AquoBasic(LloverDet, FogColor.rgb);
        ambientcolor = Ambient(LloverDet, FogColor.rgb);
    }

    gl_Position = mul(u_viewProj, vec4(worldPos, 1.0));
    #ifdef ALPHA_TEST 
    //sanke sinseksvje
    if(a_color0.g!=a_color0.b&&a_color0.r<a_color0.g+a_color0.b){
        gl_Position.x+=sin(ViewPositionAndTime.w*5.0+v_chunkPos.x+v_chunkPos.z)*0.02;
    }
    #endif
}
